package com.david.musicapp.utils;

import android.content.Context;
import android.util.Log;

import com.david.musicapp.PlayService;
import com.david.musicapp.R;
import com.david.musicapp.bean.Song;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class SongUtils {
    public static String formatTime(int time) {
        return (time / 60 > 9 ? time / 60 : ("0" + time / 60)) + ":" + (time % 60 > 9 ? time % 60 : ("0" + time % 60));
    }

    public static int getNextLoopType(int loopType) {
        switch (loopType) {
            case PlayService.SONG_ACTION_TYPE_LOOP_ONE:
                return PlayService.SONG_ACTION_TYPE_LOOP_RANDOM;
            case PlayService.SONG_ACTION_TYPE_LOOP_RANDOM:
                return PlayService.SONG_ACTION_TYPE_LOOP_NONE;
            case PlayService.SONG_ACTION_TYPE_LOOP_NONE:
                return PlayService.SONG_ACTION_TYPE_LOOP_ALL;
            default:
                return PlayService.SONG_ACTION_TYPE_LOOP_ONE;
        }
    }

    public static int getLoopImg(int loopType) {
        switch (loopType) {
            case PlayService.SONG_ACTION_TYPE_LOOP_ONE:
                return R.mipmap.ic_loop_one;
            case PlayService.SONG_ACTION_TYPE_LOOP_RANDOM:
                return R.mipmap.ic_loop_random;
            case PlayService.SONG_ACTION_TYPE_LOOP_NONE:
                return R.mipmap.ic_loop_none;
            default:
                return R.mipmap.ic_loop_all;
        }
    }


    /**
     * 数据存放在本地 * * @param tArrayList
     */
    public static void saveStorage2SDCard(Context context, ArrayList tArrayList, String fileName) {

        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        FileInputStream fileInputStream = null;
        try {

//            File file = FileUtils.getFile(File.separator + FileUtils.projectPath + File.separator + fileName);
            File file = new File(context.getExternalCacheDir().getAbsolutePath() + File.separator + fileName);
            fileOutputStream = new FileOutputStream(file.toString());  //新建一个内容为空的文件
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(tArrayList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (objectOutputStream != null) {
            try {
                objectOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 获取本地的List数据 * * @return
     */
    public static ArrayList<Song> getStorageEntities(Context context, String fileName) {
        ObjectInputStream objectInputStream = null;
        FileInputStream fileInputStream = null;
        ArrayList<Song> savedArrayList = new ArrayList<>();
        try {
            File file = new File(context.getExternalCacheDir().getAbsolutePath() + File.separator + fileName);
            fileInputStream = new FileInputStream(file.toString());
            objectInputStream = new ObjectInputStream(fileInputStream);
            savedArrayList = (ArrayList<Song>) objectInputStream.readObject();
            objectInputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (savedArrayList.size() > 0)
            Log.e("song", "getStorageEntities:" + savedArrayList.get(0).name);

        return savedArrayList;
    }
}
